package com.bid.base.common.response;

import lombok.Getter;
import lombok.Setter;
/**
 * 返回消息Json数据
 *
 * @author alanyuan
 *
 */
@Getter@Setter
public class ApiResult<E> {

    private String  msg;
    private int status;
    private E       data;
	private String       newToken;

	public ApiResult() {
		this.status = ErrorCode.SUCCESS.getStatus();
		this.msg = "";
	}

	public ApiResult(int status,String msg) {
		this.status =status;
		this.msg = msg;
	}
	public ApiResult(int status,String msg,E data) {
		this.status =status;
		this.msg = msg;
		this.data = data;
	}


    public static final ApiResult success(Object data) {
		ApiResult res = new ApiResult();
		res.data = data;
		res.msg="成功";
		return res;
	}

	public static final ApiResult error(Object data) {
		ApiResult res = new ApiResult();
		res.data = data;
		res.msg="失败";
		return res;
	}
    /**
	 * 服务端出错时的返回内容
	 * @param msg 错误描述
	 * @return
	 */
	public static final ApiResult error(String msg) {
		ApiResult res = new ApiResult();
		res.setStatus(ErrorCode.ERROR.getStatus());
		res.setMsg(msg);
		return res;
	}
	public static final ApiResult error(String msg,Object data) {
		ApiResult res = new ApiResult();
		res.setStatus(ErrorCode.ERROR.getStatus());
		res.setMsg(msg);
		res.setData(data);
		return res;
	}

	public static final ApiResult error(ErrorCode errorCode) {
		ApiResult res = new ApiResult();
		res.setStatus(errorCode.getStatus());
		res.setMsg(errorCode.getMsg());
		return res;
	}
	public static final ApiResult error(int errorCode, String msg) {
		ApiResult res = new ApiResult();
		res.setStatus(errorCode);
		res.setMsg(msg);
		return res;
	}



	@Override
	public String toString() {
		return "{\"status\":" + this.status + "\",\"msg\":\"" + this.msg + "\",\"data\":" + data + "}";
	}

	public static final ApiResult paramError(String... obj) {
		ApiResult res = new ApiResult();
		res.setMsg( (obj!=null &&obj.length>=1)?(obj[0].toString()):"参数异常");
		res.setStatus(ErrorCode.PARAM_ERROR.getStatus());
		return res;
	}
	public static final ApiResult noLogin(String... obj) {
		ApiResult res = new ApiResult();
		res.setMsg( (obj!=null &&obj.length>=1)?(obj[0].toString()):"请登录");//登陆超时或未登陆
		res.setStatus(ErrorCode.NO_LOGIN_TIME_OUT.getStatus());
		return res;
	}
//	public static final ApiResult paramError(String msg) {
//		ApiResult res = new ApiResult();
//		res.setMsg(msg);
//		res.setStatus(ErrorCode.PARAM_ERROR.getStatus());
//		return res;
//	}
}
